

<!doctype html>

<html lang="en" data-content_root="../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>skidl.part_query &#8212; SKiDL 1.2.2 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=fa44fd50" />
    <link rel="stylesheet" type="text/css" href="../../_static/bizstyle.css?v=a66e137f" />
    
    <script src="../../_static/documentation_options.js?v=fd7cadf9"></script>
    <script src="../../_static/doctools.js?v=9a2dae69"></script>
    <script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../../_static/bizstyle.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <!--[if lt IE 9]>
    <script src="_static/css3-mediaqueries.js"></script>
    <![endif]-->
  </head><body>
    <h1 id="site-title"><a href="../../../../"><img src="../../../../images/slim_banner.png" width="100%"></a></h1>
    
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SKiDL 1.2.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">skidl.part_query</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for skidl.part_query</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>

<span class="c1"># The MIT License (MIT) - Copyright (c) Dave Vandenbout.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Functions for finding/displaying parts and footprints.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="p">(</span>  <span class="c1"># isort:skip</span>
    <span class="n">absolute_import</span><span class="p">,</span>
    <span class="n">division</span><span class="p">,</span>
    <span class="n">print_function</span><span class="p">,</span>
    <span class="n">unicode_literals</span><span class="p">,</span>
<span class="p">)</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">builtins</span> <span class="kn">import</span> <span class="nb">open</span><span class="p">,</span> <span class="nb">super</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">future</span> <span class="kn">import</span> <span class="n">standard_library</span>
    <span class="n">standard_library</span><span class="o">.</span><span class="n">install_aliases</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">pass</span>

<span class="kn">from</span> <span class="nn">.logger</span> <span class="kn">import</span> <span class="n">active_logger</span>
<span class="kn">from</span> <span class="nn">.utilities</span> <span class="kn">import</span> <span class="n">export_to_all</span><span class="p">,</span> <span class="n">fullmatch</span><span class="p">,</span> <span class="n">rmv_quotes</span><span class="p">,</span> <span class="n">to_list</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;search&quot;</span><span class="p">,</span> <span class="s2">&quot;show&quot;</span><span class="p">]</span>


<span class="c1"># TODO: Use push-down automata to parse nested parenthetical expression</span>
<span class="c1">#       of AND/OR clauses for use in advanced part searching.</span>
<span class="c1">#       https://stackoverflow.com/questions/4284991/parsing-nested-parentheses-in-python-grab-content-by-level</span>


<span class="k">def</span> <span class="nf">_parse_search_terms</span><span class="p">(</span><span class="n">terms</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a regular expression for a sequence of search terms.</span>

<span class="sd">    Substitute a zero-width lookahead assertion (?= ) for each term. Thus,</span>
<span class="sd">    the &quot;abc def&quot; would become &quot;(?=.*(abc))(?=.*(def))&quot; and would match any string</span>
<span class="sd">    containing both &quot;abc&quot; and &quot;def&quot;. Or &quot;abc (def|ghi)&quot; would become</span>
<span class="sd">    &quot;(?=.*(abc))((?=.*(def|ghi))&quot; and would match any string containing</span>
<span class="sd">    &quot;abc&quot; and &quot;def&quot; or &quot;ghi&quot;. Quoted terms can be used for phrases containing</span>
<span class="sd">    whitespace.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Place the quote-delimited REs before the RE for sequences of</span>
    <span class="c1"># non-white chars to prevent the initial portion of a quoted string from being</span>
    <span class="c1"># gathered up as a non-white character sequence.</span>
    <span class="n">terms</span> <span class="o">=</span> <span class="n">terms</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>  <span class="c1"># Remove leading/trailing spaces.</span>
    <span class="n">terms</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\s*\|\s*&quot;</span><span class="p">,</span> <span class="sa">r</span><span class="s2">&quot;|&quot;</span><span class="p">,</span> <span class="n">terms</span><span class="p">)</span>  <span class="c1"># Remove spaces around OR operator.</span>
    <span class="n">terms</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;((</span><span class="se">\&quot;</span><span class="s2">.*?</span><span class="se">\&quot;</span><span class="s2">)|(\&#39;.*?\&#39;)|(\S+))\s*&quot;</span><span class="p">,</span> <span class="sa">r</span><span class="s2">&quot;(?=.*(\1))&quot;</span><span class="p">,</span> <span class="n">terms</span><span class="p">)</span>
    <span class="n">terms</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;[\&#39;</span><span class="se">\&quot;</span><span class="s2">]&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">terms</span><span class="p">)</span>  <span class="c1"># Remove quotes.</span>
    <span class="n">terms</span> <span class="o">=</span> <span class="n">terms</span> <span class="o">+</span> <span class="s2">&quot;.*&quot;</span>
    <span class="k">return</span> <span class="n">terms</span>


<div class="viewcode-block" id="search_parts_iter">
<a class="viewcode-back" href="../../rst_output/skidl.part_query.html#skidl.part_query.search_parts_iter">[docs]</a>
<span class="nd">@export_to_all</span>
<span class="k">def</span> <span class="nf">search_parts_iter</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">tool</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Return a list of (lib, part) sequences that match a regex term.&quot;&quot;&quot;</span>

    <span class="kn">import</span> <span class="nn">skidl</span>
    <span class="kn">import</span> <span class="nn">skidl.tools</span>

    <span class="kn">from</span> <span class="nn">.schlib</span> <span class="kn">import</span> <span class="n">SchLib</span>

    <span class="n">tool</span> <span class="o">=</span> <span class="n">tool</span> <span class="ow">or</span> <span class="n">skidl</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">tool</span>

    <span class="n">terms</span> <span class="o">=</span> <span class="n">_parse_search_terms</span><span class="p">(</span><span class="n">terms</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">mk_list</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Make a list out of whatever is given.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">return</span> <span class="n">l</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">l</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">l</span><span class="p">]</span>

    <span class="c1"># Gather all the lib files from all the directories in the search paths.</span>
    <span class="n">lib_files</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">lib_suffixes</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">to_list</span><span class="p">(</span><span class="n">skidl</span><span class="o">.</span><span class="n">tools</span><span class="o">.</span><span class="n">lib_suffixes</span><span class="p">[</span><span class="n">tool</span><span class="p">]))</span>
    <span class="k">for</span> <span class="n">lib_dir</span> <span class="ow">in</span> <span class="n">skidl</span><span class="o">.</span><span class="n">lib_search_paths</span><span class="p">[</span><span class="n">tool</span><span class="p">]:</span>

        <span class="c1"># Get all the library files in the search path.</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">lib_dir</span><span class="p">)</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">FileNotFoundError</span><span class="p">,</span> <span class="ne">OSError</span><span class="p">):</span>
            <span class="n">active_logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;Could not open directory &#39;</span><span class="si">{}</span><span class="s2">&#39;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">lib_dir</span><span class="p">))</span>
            <span class="n">files</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">files</span> <span class="o">=</span> <span class="p">[(</span><span class="n">lib_dir</span><span class="p">,</span> <span class="n">l</span><span class="p">)</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">files</span> <span class="k">if</span> <span class="n">l</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">lib_suffixes</span><span class="p">)]</span>
        <span class="n">lib_files</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">files</span><span class="p">)</span>

    <span class="n">num_lib_files</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lib_files</span><span class="p">)</span>

    <span class="c1"># Now search through the lib files for parts that match the search terms.</span>
    <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="p">(</span><span class="n">lib_dir</span><span class="p">,</span> <span class="n">lib_file</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lib_files</span><span class="p">):</span>

        <span class="c1"># If just entered a new lib file, yield the name of the file and</span>
        <span class="c1"># where it is within the total number of files to search.</span>
        <span class="c1"># (This is used for progress indicators.)</span>
        <span class="k">yield</span> <span class="s2">&quot;LIB&quot;</span><span class="p">,</span> <span class="n">lib_file</span><span class="p">,</span> <span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">num_lib_files</span>

        <span class="c1"># Parse the lib file to create a part library.</span>
        <span class="n">lib</span> <span class="o">=</span> <span class="n">SchLib</span><span class="p">(</span>
            <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lib_dir</span><span class="p">,</span> <span class="n">lib_file</span><span class="p">),</span> <span class="n">tool</span><span class="o">=</span><span class="n">tool</span>
        <span class="p">)</span>  <span class="c1"># Open the library file.</span>

        <span class="c1"># Search the current library for parts with the given terms.</span>
        <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">mk_list</span><span class="p">(</span>
            <span class="c1"># Get any matching parts from the library file.</span>
            <span class="n">lib</span><span class="o">.</span><span class="n">get_parts</span><span class="p">(</span><span class="n">use_backup_lib</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">search_text</span><span class="o">=</span><span class="n">terms</span><span class="p">)</span>
        <span class="p">):</span>
            <span class="c1"># Parse the part to instantiate the complete object.</span>
            <span class="n">part</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">partial_parse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

            <span class="c1"># Return part name and aliases (everything is included in aliases).</span>
            <span class="k">for</span> <span class="n">alias</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">part</span><span class="o">.</span><span class="n">aliases</span><span class="p">):</span>
                <span class="k">yield</span> <span class="s2">&quot;PART&quot;</span><span class="p">,</span> <span class="n">lib_file</span><span class="p">,</span> <span class="n">part</span><span class="p">,</span> <span class="n">alias</span></div>



<div class="viewcode-block" id="search_parts">
<a class="viewcode-back" href="../../rst_output/skidl.part_query.html#skidl.part_query.search_parts">[docs]</a>
<span class="nd">@export_to_all</span>
<span class="k">def</span> <span class="nf">search_parts</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">tool</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Print a list of parts with the regex terms within their name, alias, description or keywords.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">parts</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">search_parts_iter</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">tool</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">part</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;LIB&quot;</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="mi">79</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\r</span><span class="s2">Searching </span><span class="si">{}</span><span class="s2"> ...&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">part</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">sep</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot;</span><span class="se">\r</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">part</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;PART&quot;</span><span class="p">:</span>
            <span class="n">parts</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">part</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">4</span><span class="p">])</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="mi">79</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot;</span><span class="se">\r</span><span class="s2">&quot;</span><span class="p">)</span>

    <span class="c1"># Print each part name sorted by the library where it was found.</span>
    <span class="k">for</span> <span class="n">lib_file</span><span class="p">,</span> <span class="n">part</span><span class="p">,</span> <span class="n">part_name</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">parts</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
        <span class="nb">print</span><span class="p">(</span>
            <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">: </span><span class="si">{}</span><span class="s2"> (</span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">lib_file</span><span class="p">,</span> <span class="n">part_name</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">part</span><span class="p">,</span> <span class="s2">&quot;description&quot;</span><span class="p">,</span> <span class="s2">&quot;???&quot;</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span></div>



<div class="viewcode-block" id="show_part">
<a class="viewcode-back" href="../../rst_output/skidl.part_query.html#skidl.part_query.show_part">[docs]</a>
<span class="nd">@export_to_all</span>
<span class="k">def</span> <span class="nf">show_part</span><span class="p">(</span><span class="n">lib</span><span class="p">,</span> <span class="n">part_name</span><span class="p">,</span> <span class="n">tool</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Print the I/O pins for a given part in a library.</span>

<span class="sd">    Args:</span>
<span class="sd">        lib: Either a SchLib object or the name of a library.</span>
<span class="sd">        part_name: The name of the part in the library.</span>
<span class="sd">        tool: The ECAD tool format for the library.</span>

<span class="sd">    Returns:</span>
<span class="sd">        A Part object.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="kn">import</span> <span class="nn">skidl</span>

    <span class="kn">from</span> <span class="nn">.part</span> <span class="kn">import</span> <span class="n">TEMPLATE</span><span class="p">,</span> <span class="n">Part</span>

    <span class="n">tool</span> <span class="o">=</span> <span class="n">tool</span> <span class="ow">or</span> <span class="n">skidl</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">tool</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">Part</span><span class="p">(</span><span class="n">lib</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">part_name</span><span class="p">),</span> <span class="n">tool</span><span class="o">=</span><span class="n">tool</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="n">TEMPLATE</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span></div>



<span class="k">class</span> <span class="nc">FootprintCache</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Dict for storing footprints from all directories.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>  <span class="c1"># Cache starts off empty, hence invalid.</span>

    <span class="k">def</span> <span class="nf">reset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>  <span class="c1"># Clear out cache.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">valid</span> <span class="o">=</span> <span class="kc">False</span>  <span class="c1"># Cache is empty, hence invalid.</span>

    <span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Load cache with footprints from libraries in fp-lib-table file.&quot;&quot;&quot;</span>

        <span class="c1"># Expand any env. vars and/or user in the path.</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expandvars</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>

        <span class="c1"># Read contents of footprint library file into a single string.</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># Look for fp-lib-table file and read its entries into a table of footprint module libs.</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s2">&quot;fp-lib-table&quot;</span><span class="p">))</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
                <span class="n">tbl</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">FileNotFoundError</span><span class="p">:</span>
            <span class="c1"># fp-lib-table file was not found, so create a table containing the path directory</span>
            <span class="c1"># as a single module lib.</span>
            <span class="n">nickname</span><span class="p">,</span> <span class="n">ext</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
            <span class="n">tbl</span> <span class="o">=</span> <span class="s1">&#39;(fp_lib_table</span><span class="se">\n</span><span class="s1">(lib (name </span><span class="si">{nickname}</span><span class="s1">)(type KiCad)(uri </span><span class="si">{path}</span><span class="s1">)(options &quot;&quot;)(descr &quot;&quot;))</span><span class="se">\n</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="o">**</span><span class="nb">locals</span><span class="p">()</span>
            <span class="p">)</span>

        <span class="c1"># Get individual &quot;(lib ...)&quot; entries from the string.</span>
        <span class="n">libs</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span>
            <span class="sa">r</span><span class="s2">&quot;\(\s*lib\s* .*? \)\)&quot;</span><span class="p">,</span> <span class="n">tbl</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">VERBOSE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span>
        <span class="p">)</span>

        <span class="c1"># Add the footprint modules found in each enabled KiCad library.</span>
        <span class="k">for</span> <span class="n">lib</span> <span class="ow">in</span> <span class="n">libs</span><span class="p">:</span>

            <span class="c1"># Skip disabled libraries.</span>
            <span class="n">disabled</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span>
                <span class="sa">r</span><span class="s2">&quot;\(\s*disabled\s*\)&quot;</span><span class="p">,</span> <span class="n">lib</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">VERBOSE</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">disabled</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="c1"># Skip non-KiCad libraries (primarily git repos).</span>
            <span class="n">type_</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span>
                <span class="sa">r</span><span class="s1">&#39;(?:\(\s*type\s*) (&quot;[^&quot;]*?&quot;|[^)]*?) (?:\s*\))&#39;</span><span class="p">,</span>
                <span class="n">lib</span><span class="p">,</span>
                <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">VERBOSE</span><span class="p">,</span>
            <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">type_</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="s2">&quot;kicad&quot;</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="c1"># Get the library directory and nickname.</span>
            <span class="n">uri</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span>
                <span class="sa">r</span><span class="s1">&#39;(?:\(\s*uri\s*) (&quot;[^&quot;]*?&quot;|[^)]*?) (?:\s*\))&#39;</span><span class="p">,</span>
                <span class="n">lib</span><span class="p">,</span>
                <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">VERBOSE</span><span class="p">,</span>
            <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">nickname</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span>
                <span class="sa">r</span><span class="s1">&#39;(?:\(\s*name\s*) (&quot;[^&quot;]*?&quot;|[^)]*?) (?:\s*\))&#39;</span><span class="p">,</span>
                <span class="n">lib</span><span class="p">,</span>
                <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">VERBOSE</span><span class="p">,</span>
            <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

            <span class="c1"># Remove any quotes around the URI or nickname.</span>
            <span class="n">uri</span> <span class="o">=</span> <span class="n">rmv_quotes</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
            <span class="n">nickname</span> <span class="o">=</span> <span class="n">rmv_quotes</span><span class="p">(</span><span class="n">nickname</span><span class="p">)</span>

            <span class="c1"># Expand environment variables and ~ in the URI.</span>
            <span class="n">uri</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expandvars</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="n">uri</span><span class="p">))</span>

            <span class="c1"># Look for unexpanded env vars and skip this loop iteration if found.</span>
            <span class="k">def</span> <span class="nf">get_env_vars</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
<span class="w">                </span><span class="sd">&quot;&quot;&quot;Return a list of environment variables found in a string.&quot;&quot;&quot;</span>
                <span class="n">env_vars</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">env_var_re</span> <span class="ow">in</span> <span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\${([^}]*)}&quot;</span><span class="p">,</span> <span class="sa">r</span><span class="s2">&quot;\$(\w+)&quot;</span><span class="p">,</span> <span class="sa">r</span><span class="s2">&quot;%(\w+)%&quot;</span><span class="p">):</span>
                    <span class="n">env_vars</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">env_var_re</span><span class="p">,</span> <span class="n">s</span><span class="p">))</span>
                <span class="k">return</span> <span class="n">env_vars</span>

            <span class="n">unexpanded_vars</span> <span class="o">=</span> <span class="n">get_env_vars</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">unexpanded_vars</span><span class="p">:</span>
                <span class="n">active_logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                    <span class="s2">&quot;There are some undefined environment variables: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">unexpanded_vars</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="k">continue</span>

            <span class="c1"># Get a list of all the footprint module files in the top-level of the library URI.</span>
            <span class="n">filenames</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">fn</span>
                <span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">uri</span><span class="p">,</span> <span class="n">fn</span><span class="p">))</span>
                <span class="ow">and</span> <span class="n">fn</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;.kicad_mod&quot;</span><span class="p">)</span>
            <span class="p">]</span>

            <span class="c1"># Create an entry in the cache for this nickname. (This will overwrite</span>
            <span class="c1"># any previous nickname entry, so make sure to scan fp-lib-tables in order of</span>
            <span class="c1"># increasing priority.) Each entry contains the path to the directory containing</span>
            <span class="c1"># the footprint module and a dictionary of the modules keyed by the module name</span>
            <span class="c1"># with an associated value containing the module file contents (which starts off</span>
            <span class="c1"># as None).</span>
            <span class="bp">self</span><span class="p">[</span><span class="n">nickname</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s2">&quot;path&quot;</span><span class="p">:</span> <span class="n">uri</span><span class="p">,</span>
                <span class="s2">&quot;modules&quot;</span><span class="p">:</span> <span class="p">{</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">fn</span><span class="p">)[</span><span class="mi">0</span><span class="p">]:</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="n">filenames</span><span class="p">},</span>
            <span class="p">}</span>


<span class="c1"># Cache for storing footprints read from .kicad_mod files.</span>
<span class="n">footprint_cache</span> <span class="o">=</span> <span class="n">FootprintCache</span><span class="p">()</span>


<div class="viewcode-block" id="search_footprints_iter">
<a class="viewcode-back" href="../../rst_output/skidl.part_query.html#skidl.part_query.search_footprints_iter">[docs]</a>
<span class="nd">@export_to_all</span>
<span class="k">def</span> <span class="nf">search_footprints_iter</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">tool</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Return a list of (lib, footprint) sequences that match a regex term.&quot;&quot;&quot;</span>

    <span class="kn">import</span> <span class="nn">skidl</span>

    <span class="n">tool</span> <span class="o">=</span> <span class="n">tool</span> <span class="ow">or</span> <span class="n">skidl</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">tool</span>

    <span class="n">terms</span> <span class="o">=</span> <span class="n">_parse_search_terms</span><span class="p">(</span><span class="n">terms</span><span class="p">)</span>

    <span class="c1"># If the cache isn&#39;t valid, then make it valid by gathering all the</span>
    <span class="c1"># footprint files from all the directories in the search paths.</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">footprint_cache</span><span class="o">.</span><span class="n">valid</span><span class="p">:</span>
        <span class="n">footprint_cache</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">skidl</span><span class="o">.</span><span class="n">footprint_search_paths</span><span class="p">[</span><span class="n">tool</span><span class="p">]:</span>
            <span class="n">footprint_cache</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

    <span class="c1"># Get the number of footprint libraries to be searched..</span>
    <span class="n">num_fp_libs</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">footprint_cache</span><span class="p">)</span>

    <span class="c1"># Now search through the libraries for footprints that match the search terms.</span>
    <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">fp_lib</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">footprint_cache</span><span class="p">):</span>

        <span class="c1"># If just entered a new library, yield the name of the lib and</span>
        <span class="c1"># where it is within the total number of libs to search.</span>
        <span class="c1"># (This is used for progress indicators.)</span>
        <span class="k">yield</span> <span class="s2">&quot;LIB&quot;</span><span class="p">,</span> <span class="n">fp_lib</span><span class="p">,</span> <span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">num_fp_libs</span>

        <span class="c1"># Get path to library directory and dict of footprint modules.</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">footprint_cache</span><span class="p">[</span><span class="n">fp_lib</span><span class="p">][</span><span class="s2">&quot;path&quot;</span><span class="p">]</span>
        <span class="n">modules</span> <span class="o">=</span> <span class="n">footprint_cache</span><span class="p">[</span><span class="n">fp_lib</span><span class="p">][</span><span class="s2">&quot;modules&quot;</span><span class="p">]</span>

        <span class="c1"># Search each module in the library.</span>
        <span class="k">for</span> <span class="n">module_name</span> <span class="ow">in</span> <span class="n">modules</span><span class="p">:</span>

            <span class="c1"># If the cache isn&#39;t valid, then read each footprint file and store</span>
            <span class="c1"># it&#39;s contents in the cache.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">footprint_cache</span><span class="o">.</span><span class="n">valid</span><span class="p">:</span>
                <span class="n">file</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">module_name</span> <span class="o">+</span> <span class="s2">&quot;.kicad_mod&quot;</span><span class="p">)</span>
                <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="c1"># Remove any linefeeds that would interfere with fullmatch() later on.</span>
                        <span class="n">modules</span><span class="p">[</span><span class="n">module_name</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">l</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">fp</span><span class="o">.</span><span class="n">readlines</span><span class="p">()]</span>
                    <span class="k">except</span> <span class="ne">UnicodeDecodeError</span><span class="p">:</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">modules</span><span class="p">[</span><span class="n">module_name</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span>
                                <span class="n">l</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="n">fp</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
                            <span class="p">]</span>
                        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                            <span class="n">modules</span><span class="p">[</span><span class="n">module_name</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>

            <span class="c1"># Get the contents of the footprint file from the cache.</span>
            <span class="n">module_text</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">modules</span><span class="p">[</span><span class="n">module_name</span><span class="p">])</span>

            <span class="c1"># Count the pads so it can be added to the text being searched.</span>
            <span class="c1"># Join all the module text lines, search for the number of</span>
            <span class="c1"># occurrences of &quot;(pad&quot;, and then count them.</span>
            <span class="c1"># A set is used so pads with the same num/name are only counted once.</span>
            <span class="c1"># Place the pad count before everything else so the space that</span>
            <span class="c1"># terminates it won&#39;t be stripped off later. This is necessary</span>
            <span class="c1"># so (for example) &quot;#pads=20 &quot; won&#39;t match &quot;#pads=208&quot;.</span>
            <span class="n">num_pads</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span>
                <span class="nb">set</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\(\s*pad\s+([^\s)]+)&quot;</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">module_text</span><span class="p">)))</span>
            <span class="p">)</span>
            <span class="n">num_pads_str</span> <span class="o">=</span> <span class="s2">&quot;#pads=</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num_pads</span><span class="p">)</span>

            <span class="c1"># Create a string with the module name, library name, number of pads,</span>
            <span class="c1"># description and tags.</span>
            <span class="n">search_text</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">num_pads_str</span><span class="p">,</span> <span class="n">fp_lib</span><span class="p">,</span> <span class="n">module_name</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">module_text</span><span class="p">:</span>
                <span class="k">if</span> <span class="s2">&quot;(descr &quot;</span> <span class="ow">in</span> <span class="n">line</span> <span class="ow">or</span> <span class="s2">&quot;(tags &quot;</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
                    <span class="n">search_text</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">search_text</span><span class="p">,</span> <span class="n">line</span><span class="p">])</span>

            <span class="c1"># Search the string for a match with the search terms.</span>
            <span class="k">if</span> <span class="n">fullmatch</span><span class="p">(</span>
                <span class="n">terms</span><span class="p">,</span> <span class="n">search_text</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">MULTILINE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span>
            <span class="p">):</span>
                <span class="k">yield</span> <span class="s2">&quot;MODULE&quot;</span><span class="p">,</span> <span class="n">fp_lib</span><span class="p">,</span> <span class="n">module_text</span><span class="p">,</span> <span class="n">module_name</span>

    <span class="c1"># At the end, all modules have been scanned and the footprint cache is valid.</span>
    <span class="n">footprint_cache</span><span class="o">.</span><span class="n">valid</span> <span class="o">=</span> <span class="kc">True</span></div>



<div class="viewcode-block" id="search_footprints">
<a class="viewcode-back" href="../../rst_output/skidl.part_query.html#skidl.part_query.search_footprints">[docs]</a>
<span class="nd">@export_to_all</span>
<span class="k">def</span> <span class="nf">search_footprints</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">tool</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Print a list of footprints with the regex term within their description/tags.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">footprints</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">fp</span> <span class="ow">in</span> <span class="n">search_footprints_iter</span><span class="p">(</span><span class="n">terms</span><span class="p">,</span> <span class="n">tool</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">fp</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;LIB&quot;</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="mi">79</span><span class="p">,</span> <span class="s2">&quot;</span><span class="se">\r</span><span class="s2">Searching </span><span class="si">{}</span><span class="s2"> ...&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">fp</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">sep</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot;</span><span class="se">\r</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">fp</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;MODULE&quot;</span><span class="p">:</span>
            <span class="n">footprints</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fp</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">4</span><span class="p">])</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="mi">79</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot;</span><span class="se">\r</span><span class="s2">&quot;</span><span class="p">)</span>

    <span class="c1"># Print each module name sorted by the library where it was found.</span>
    <span class="k">for</span> <span class="n">lib_file</span><span class="p">,</span> <span class="n">module_text</span><span class="p">,</span> <span class="n">module_name</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span>
        <span class="n">footprints</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">f</span><span class="p">:</span> <span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">f</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
    <span class="p">):</span>
        <span class="n">descr</span> <span class="o">=</span> <span class="s2">&quot;???&quot;</span>
        <span class="n">tags</span> <span class="o">=</span> <span class="s2">&quot;???&quot;</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">module_text</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">descr</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;(descr &quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s2">&quot;)&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">tags</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;(tags &quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s2">&quot;)&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">: </span><span class="si">{}</span><span class="s2"> (</span><span class="si">{}</span><span class="s2"> - </span><span class="si">{}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">lib_file</span><span class="p">,</span> <span class="n">module_name</span><span class="p">,</span> <span class="n">descr</span><span class="p">,</span> <span class="n">tags</span><span class="p">))</span></div>



<div class="viewcode-block" id="show_footprint">
<a class="viewcode-back" href="../../rst_output/skidl.part_query.html#skidl.part_query.show_footprint">[docs]</a>
<span class="nd">@export_to_all</span>
<span class="k">def</span> <span class="nf">show_footprint</span><span class="p">(</span><span class="n">lib</span><span class="p">,</span> <span class="n">module_name</span><span class="p">,</span> <span class="n">tool</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Print the pads for a given module in a library.</span>

<span class="sd">    Args:</span>
<span class="sd">        lib: The name of a library.</span>
<span class="sd">        module_name: The name of the footprint in the library.</span>
<span class="sd">        tool: The ECAD tool format for the library.</span>

<span class="sd">    Returns:</span>
<span class="sd">        A Part object.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="kn">import</span> <span class="nn">skidl</span>

    <span class="n">tool</span> <span class="o">=</span> <span class="n">tool</span> <span class="ow">or</span> <span class="n">skidl</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">tool</span>

    <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;KISYSMOD&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">pathsep</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">skidl</span><span class="o">.</span><span class="n">footprint_search_paths</span><span class="p">[</span><span class="n">tool</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">pym</span><span class="o">.</span><span class="n">Module</span><span class="o">.</span><span class="n">from_library</span><span class="p">(</span><span class="n">lib</span><span class="p">,</span> <span class="n">module_name</span><span class="p">)</span></div>



<span class="c1"># Define some shortcuts.</span>
<span class="n">search</span> <span class="o">=</span> <span class="n">search_parts</span>
<span class="n">show</span> <span class="o">=</span> <span class="n">show_part</span>
</pre></div>

            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SKiDL 1.2.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >Module code</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">skidl.part_query</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
    &#169; Copyright 2016-2024, Dave Vandenbout.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.3.7.
    </div>
  </body>
</html>